/**
 * Created by DoveXL on 2018/12/7.
 */


$(document).ready(function () {
    queryBank(null,null,null,null,null);
});



function queryUserByCondition() {
    var volunteerunit = $("#volunteerunit").val();
    var volunteername = $("#volunteername").val();
    var IDCardNumber = $("#IDCardNumber").val();
    if (volunteerunit=="")
    {
        volunteerunit=null;
    }else
    {
        volunteerunit = "%"+volunteerunit+"%";
    }

    if(volunteername=="")
    {
        volunteername=null
    }else
    {
        volunteername="%"+volunteername+"%";
    }

    if(IDCardNumber=="")
    {
        IDCardNumber=null;
    }else
    {
        IDCardNumber="%"+IDCardNumber+"%";
    }
    queryBank(volunteerunit,IDCardNumber,volunteername,null,null);
}



function queryBank(Unit,IDCard,Name,Account,Branch) {
    $.ajax({
        async: true,
        type: "post",
        url: "/VolunteerPlatform/BankInfo/CertainBankInfo",
        dataType: "json",
        contentType : 'application/json;charset=utf-8',
        data: JSON.stringify({"page":1,"pagesize":8,"Unit":Unit,"IDCard":IDCard,"Name":Name,"Account":Account,"Branch":Branch}),
        cache: false,
        success: function(data) {
            var result = eval(data);
            $("#BankManagementInfo tr").empty();
            for (var i = 0; i<result.pagelist.length; i++) {
                var row = CreateBankRow(result.pagelist[i]);
                $("#BankManagementInfo").append(row);
            };
            var currentPage = result.currentpage; //当前页数
            var pageCount = result.pagecount; //总页数
            var options = {
                bootstrapMajorVersion: 3, //版本
                currentPage: currentPage, //当前页数
                totalPages: pageCount, //总页数
                numberOfPages: 8,
                shouldShowPage:true,//是否显示该按钮
                itemTexts: function (type, page, current) {
                    switch (type) {
                        case "first":
                            return "首页";
                        case "prev":
                            return "上一页";
                        case "next":
                            return "下一页";
                        case "last":
                            return "末页";
                        case "page":
                            return page;
                    }
                },
                onPageClicked: function (event, originalEvent, type, page) {
                    $.ajax({
                        async: true,
                        url: "/VolunteerPlatform/BankInfo/CertainBankInfo",
                        type: "post",
                        dataType : "json",
                        contentType : 'application/json;charset=utf-8',
                        data: JSON.stringify({"page":page,"pagesize":8,"IDCard":IDCard,"Name":Name,"Unit":Unit,"Account":Account,"Branch":Branch}),
                        cache: false,
                        success: function (data) {
                            $("#BankManagementInfo tr").empty();
                            var result = eval(data);
                            for (var i = 0; i <result.pagelist.length; i++) {
                                var row = CreateBankRow(result.pagelist[i]);
                                $("#BankManagementInfo").append(row);
                            };
                        }
                    });
                }
            };
            $('#UserpageLimit').bootstrapPaginator(options);
        }
    });
}




function CreateBankRow(UserInfo) {
    var row = document.createElement('tr');
    var ChooseCell = document.createElement('td');
    var btnLabel = document.createElement('label');
    var btnCheckbox = document.createElement('input');
    btnCheckbox.setAttribute("type","checkbox");
    btnCheckbox.setAttribute("name","BankCheckbox");
    btnLabel.appendChild(btnCheckbox);
    ChooseCell.appendChild(btnLabel);
    row.appendChild(ChooseCell);
    var UserNameCell = document.createElement('td');
    UserNameCell.innerHTML = UserInfo.user_name;
    row.appendChild(UserNameCell);
    var UserIDCardCell = document.createElement('td');
    UserIDCardCell.innerHTML = UserInfo.user_IDCard;
    row.appendChild(UserIDCardCell);
    var UserAccountCell = document.createElement('td');
    UserAccountCell.innerHTML = UserInfo.user_bankaccount;
    row.appendChild(UserAccountCell);
    var UserBranchCell = document.createElement('td');
    UserBranchCell.innerHTML = UserInfo.user_bankbranch;
    row.appendChild(UserBranchCell);
    return row;
}



function ShowChangeBankInfoModal() {
    var count = $("input[name = 'BankCheckbox']:checked").length;
    if(count==0)
    {
        alert("未选中任何行");
    }else if(count>1)
    {
        alert("选中超过一行!");
    }else
    {
        var CheckedNode = $("input[name = 'BankCheckbox']:checked").parent().parent();
        var Name = $(CheckedNode).next().html().trim();
        var IDCardNumber = $(CheckedNode).next().next().html().trim();
        var BankAccount = $(CheckedNode).next().next().next().html().trim();
        var BankBranch= $(CheckedNode).next().next().next().next().html().trim();
        if(BankAccount=="无")
        {
            BankAccount="";
        }
        if(BankBranch=="无")
        {
            BankBranch="";
        }
        $("#ChangePersonName").html(Name);
        $("#ChangeIDCardNumber").html(IDCardNumber);
        $("#ChangeBankAccount").val(BankAccount);
        $("#ChangeBankBranch").val(BankBranch);
        $("#ChangeBankInfoFlag").html("");
        $("#BtnChangeBankInfo").removeAttr("disabled");
        $("#BtnChangeBankInfo").removeClass("disabled");
        $("#ChangeBankInfoModal").modal();
    }
}




function ChangeBankInfoFunction() {
    var CheckedNode = $("input[name = 'BankCheckbox']:checked").parent().parent();
    var NameTest = $(CheckedNode).next().html().trim();
    var IDCardNumberTest = $(CheckedNode).next().next().html().trim();
    var BankAccountTest = $(CheckedNode).next().next().next().html().trim();
    var BankBranchTest = $(CheckedNode).next().next().next().next().html().trim();
    if (BankAccountTest == "无") {
        BankAccountTest = "";
    }
    if (BankBranchTest == "无") {
        BankBranchTest = "";
    }
    var BankAccount= $("#ChangeBankAccount").val().trim();
    var BankBranch = $("#ChangeBankBranch").val().trim();
    if (BankAccount == BankAccountTest && BankBranchTest == BankBranch) {
        $("#ChangeTeamInfoFlag").html("未修改任何信息，不能提交!");
        return false;
    }else
    {
        if(BankAccount.length==0)
        {
            BankAccount = "无";
            if(BankBranch.length==0)
            {
                BankBranch="无";
            }
            if(checkChinese("开户银行",BankBranch))
            {
                $.ajax({
                    async: true,
                    type: "post",
                    url: "/VolunteerPlatform/BankInfo/ChangeBankInfo",
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify({
                        "IDCardNumber": IDCardNumberTest,
                        "Name": NameTest,
                        "BankAccount": BankAccount,
                        "BankBranch": BankBranch
                    }),
                    cache: false,
                    success: function (data) {
                        if(data=="true")
                        {
                            $("#ChangeBankInfoFlag").html("修改/完善成功!");
                            $("#BtnChangeBankInfo").attr("disabled","disabled");
                            $("#BtnChangeBankInfo").addClass("disabled","disabled");
                            queryUserByCondition();
                        }else if(data=="false")
                        {
                            $("#ChangeBankInfoFlag").html("修改/完善失败!");
                        }else
                        {
                            $("#ChangeBankInfoFlag").html("其他原因导致的修改/完善失败!");
                        }

                    }})
            }
            else
            {
                $("#ChangeBankInfoFlag").html("开户行不全是中文，不正确!");
                    return false;
            }
        }else
        {
            if(luhnCheck(BankAccount))
            {
                if(BankBranch.length==0)
                {
                    BankBranch="无";
                }
                if(checkChinese("开户银行",BankBranch))
                {
                    $.ajax({
                        async: true,
                        type: "post",
                        url: "/VolunteerPlatform/BankInfo/ChangeBankInfo",
                        contentType: 'application/json;charset=utf-8',
                        data: JSON.stringify({
                            "IDCardNumber": IDCardNumberTest,
                            "Name": NameTest,
                            "BankAccount": BankAccount,
                            "BankBranch": BankBranch
                        }),
                        cache: false,
                        success: function (data) {
                            if(data=="true")
                            {
                                $("#ChangeBankInfoFlag").html("修改/完善成功!");
                                $("#BtnChangeBankInfo").attr("disabled","disabled");
                                $("#BtnChangeBankInfo").addClass("disabled","disabled");
                                queryUserByCondition();
                            }else if(data=="false")
                            {
                                $("#ChangeBankInfoFlag").html("修改/完善失败!");
                            }else
                            {
                                $("#ChangeBankInfoFlag").html("其他原因导致的修改/完善失败!");
                            }
                        }})
                }
            }else
            {
                $("#ChangeBankInfoFlag").html("银行账户不正确!");
                return false;
            }
        }
    }
}


function ShowDeleteBankInfoModal() {
    var count = $("input[name = 'BankCheckbox']:checked").length;
    if(count==0)
    {
        alert("未选中任何行");
        return false;
    }else if(count>1)
    {
        alert("选中超过一行!");
        return false;
    }else {
        var CheckedNode = $("input[name = 'BankCheckbox']:checked").parent().parent();
        var Name = $(CheckedNode).next().html().trim();
        var IDCardNumber = $(CheckedNode).next().next().html().trim();
        var BankAccount = $(CheckedNode).next().next().next().html().trim();
        var BankBranch = $(CheckedNode).next().next().next().next().html().trim();
        if (BankBranch == "无" && BankAccount == "无") {
            alert("该用户无账户!");
            return false;
        } else {
        $("#DeletePersonName").html(Name);
        $("#DeleteIDCardNumber").html(IDCardNumber);
        $("#DeleteBankAccount").html(BankAccount);
        $("#DeleteBankBranch").html(BankBranch);
        $("#DeleteBankInfoFlag").html("");
        $("#BtnDeleteBankInfo").removeAttr("disabled");
        $("#BtnDeleteBankInfo").removeClass("disabled");
        $("#DeleteBankInfoModal").modal();
        }
    }
}


function DeleteBankInfoFunction() {

    var CheckedNode = $("input[name = 'BankCheckbox']:checked").parent().parent();
    var NameTest = $(CheckedNode).next().html().trim();
    var IDCardNumberTest = $(CheckedNode).next().next().html().trim();
    var BankAccount ="无";
    var BankBranch = "无";
    $.ajax({
        async: true,
        type: "post",
        url: "/VolunteerPlatform/BankInfo/ChangeBankInfo",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify({
            "IDCardNumber": IDCardNumberTest,
            "Name": NameTest,
            "BankAccount": BankAccount,
            "BankBranch": BankBranch
        }),
        cache: false,
        success: function (data) {
            if(data=="true")
            {
                $("#DeleteBankInfoFlag").html("删除成功!");
                $("#BtnDeleteBankInfo").attr("disabled","disabled");
                $("#BtnDeleteBankInfo").addClass("disabled","disabled");
                queryUserByCondition();
            }else if(data=="false")
            {
                $("#DeleteBankInfoFlag").html("删除失败!");
            }else
            {
                $("#DeleteBankInfoFlag").html("其他原因导致的删除失败!");
            }
        }})
}






function ShowExportBankInfoModal() {
    $("#ExportBankInfoExcel").css("display","none");
    $("#ExportBankInfoModal").modal();
}


function ExportExcelContent()
{
    var volunteerunit = $("#volunteerunit").val().trim();
    var volunteername = $("#volunteername").val().trim();
    var IDCardNumber = $("#IDCardNumber").val().trim();
    if (volunteerunit.length==0)
    {
        volunteerunit=null;
    }else
    {
        volunteerunit = "%"+volunteerunit+"%";
    }

    if(volunteername.length==0)
    {
        volunteername=null
    }else
    {
        volunteername="%"+volunteername+"%";
    }

    if(IDCardNumber.length==0)
    {
        IDCardNumber=null;
    }else
    {
        IDCardNumber="%"+IDCardNumber+"%";
    }
    var checklist=[];
    checklist.push("Number");
    $("input[name = 'ExportField']:checked").each(function () {
            checklist.push($(this).val());
        }
    );
    if(checklist.length==2)
    {
        alert("任何一个字段都未选中!");
        return false;
    }
    $.ajax({
        async: false,
        type: "post",
        url: "/VolunteerPlatform/BankInfo/ExportBankInfo",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify({"checkFieldName": checklist, "IDCardNumber":IDCardNumber, "Name":volunteername, "Unit":volunteerunit}),
        success: function (data) {
            if(data=="1")
            {
                $("#ExportBankInfoExcel").css("display","block");
            }else
            {
            }
        }
    });
}

function DisplayDownload() {

    $("#ExportBankInfoExcel").css("display","none");
}
